package com.ego.controller;

import com.ego.pojo.Sku;
import com.ego.pojo.SpuDetail;
import com.ego.pojo.Stock;
import com.ego.pojo.vo.ResultVO;
import com.ego.pojo.bo.SpuBo;
import com.ego.service.SpuService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author Ares
 * @date 2020-11-25
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private SpuService spuService;

    @GetMapping("/spu/page")
    public ResponseEntity<ResultVO<SpuBo>> getSpuBo(
            @RequestParam("key")String key,
            @RequestParam(value = "saleable",required = false)Boolean saleable,
            @RequestParam("page")Integer page,
            @RequestParam("rows")Integer rows){
        ResultVO<SpuBo> vo = spuService.getSpuBo(key, saleable, page, rows);
        return ResponseEntity.ok(vo);
    }

    @GetMapping("/spu/pageForES")
    public ResponseEntity<ResultVO<SpuBo>> page(
            @RequestParam(value = "saleable",required = false)Boolean saleable,
            @RequestParam("page")Integer page,
            @RequestParam("rows")Integer rows){
        ResultVO<SpuBo> vo = spuService.page(saleable, page, rows);
        return ResponseEntity.ok(vo);
    }

    @DeleteMapping
    public ResponseEntity<Void> deleteSpu(@RequestParam("id")Long id){
        spuService.deleteSpu(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/spu/detail/{sid}")
    public ResponseEntity<SpuDetail> getSpuDetailById(@PathVariable Long sid){
        return ResponseEntity.ok(spuService.getSpuDetailById(sid));
    }

    @PostMapping
    public ResponseEntity<Void> saveGoods(@RequestBody SpuBo spuBo){
        spuService.saveGoods(spuBo);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/skus")
    public ResponseEntity<List<Sku>> getSkus(@RequestParam("spuId") Long spuId){
        return ResponseEntity.ok(spuService.getSkus(spuId));
    }

    @PutMapping
    public ResponseEntity<Void> updateGoods(@RequestBody SpuBo spuBo){
        spuService.updateGoods(spuBo);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/editSaleable")
    public ResponseEntity<Void> editSaleable(@RequestBody SpuBo spuBo){
        spuService.editSaleable(spuBo);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/spubo/{id}")
    public SpuBo queryGoodsById(@PathVariable("id") Long id){
        return spuService.queryGoodsById(id);
    }

    @GetMapping("/sku/{id}")
    public ResponseEntity<Sku> querySkuById(@PathVariable("id") Long skuId){
        Sku sku=spuService.querySkuById(skuId);
        if (sku == null){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(sku);
    }

    @GetMapping("/skuList")
    public ResponseEntity<List<Sku>> querySkuListByIds(@RequestParam("ids") List<Long> skuIdList){
        List<Sku> result=spuService.querySkuListByIds(skuIdList);
        if (result == null){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping("/descrementStock")
    ResponseEntity<Void>  descrementStock(@RequestBody Map<Long, Integer> stockData){
        spuService.descrementStock(stockData);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/stocks")
    ResponseEntity<List<Stock>> queryStockListByIds(@RequestParam("skuIdList") List<Long> skuIdList){
        return ResponseEntity.ok(spuService.findStockListBySkuIdList(skuIdList));
    }

    @PostMapping("stock/seckill/decrease/{skuId}")
    ResponseEntity<Void> decreaseSeckillStock(@PathVariable("skuId") Long skuId){
        spuService.descrementSeckillStock(skuId);
        return ResponseEntity.ok().build();
    }
}
